﻿using System;
using Core.View.Renders;
using Core.Controllers;
using Core.Model.Chunks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNA.Game.Model;
using XNA.Game.Model.Helpers;
using Core.Model.Types;

namespace XNA.Game.View.Renders
{
    public class XnaWordRenderer : WorldRenderer
    {
        #region Constants

        public Vector4 NIGHTCOLOR = Color.Black.ToVector4();
        public Vector4 SUNCOLOR = Color.White.ToVector4();
        public Vector4 HORIZONCOLOR = Color.White.ToVector4();

        public Vector4 EVENINGTINT = Color.Red.ToVector4();
        public Vector4 MORNINGTINT = Color.Gold.ToVector4();

        #endregion

        #region Fields

        private GameCicle Game;

        private Effect Effect;

        #endregion

        #region .ctor

        public XnaWordRenderer(GameCicle game)
        {
            Game = game;
        }

        #endregion

        #region Properies

        public override IntPtr WindowHandle
        {
            get { return Game.Window.Handle; }
        }

        public override int WindowWidth
        {
            get { return Game.Window.ClientBounds.Width; }
        }

        public override int WindowHeight
        {
            get { return Game.Window.ClientBounds.Height; }
        }

        #endregion

        #region Public methods

        public void LoadContent(ContentManager content)
        {
            Effect = content.Load<Effect>("Content/Shaders/triangle");

            Texture2D textureAtlas = content.Load<Texture2D>("Content/Textures/terrain1");

            Effect.Parameters["FogNear"].SetValue(ChunkManager.FOGNEAR);
            Effect.Parameters["FogFar"].SetValue(ChunkManager.FOGFAR);
            Effect.Parameters["Texture1"].SetValue(textureAtlas);

            Effect.Parameters["HorizonColor"].SetValue(HORIZONCOLOR);
            Effect.Parameters["NightColor"].SetValue(NIGHTCOLOR);

            Effect.Parameters["MorningTint"].SetValue(MORNINGTINT);
            Effect.Parameters["EveningTint"].SetValue(EVENINGTINT);

            Effect.Parameters["SunColor"].SetValue(SUNCOLOR);
        }

        public override void BeginDraw()
        {
            Game.GraphicsDevice.Clear(Color.White);
        }

        public override void DrawTerrain(Camera camera, Chunk[] chunks)
        {
            Effect.Parameters["World"].SetValue(TypeCompatibleHelper.ConvertFromMatrixF(camera.World));
            Effect.Parameters["View"].SetValue(TypeCompatibleHelper.ConvertFromMatrixF(camera.View));
            Effect.Parameters["Projection"].SetValue(TypeCompatibleHelper.ConvertFromMatrixF(camera.Projection));
            Effect.Parameters["CameraPosition"].SetValue(TypeCompatibleHelper.ConvertFromVector3F(camera.Position));

            BoundingFrustumF viewFrustum = new BoundingFrustumF(camera.View * camera.Projection);

            for (int i = 0; i < chunks.Length; i++)
            {
                Chunk chunk = chunks[i];

                if (chunk == null || chunk.IsReady == false)
                {
                    continue;
                }

                if (viewFrustum.FastIntersects(chunk.BoundingBox))
                {
                    Effect.Parameters["Index"].SetValue(TypeCompatibleHelper.ConvertFromVector3F(chunk.Index3));

                    ((XnaChunk)chunk).DrawChunk(Game.GraphicsDevice, Effect);
                }
            }
        }

        public override void EndDraw()
        {
        }

        #endregion

    }
}
